//package test.data_structure.hashmap;
//
//import java.util.HashMap;
//import java.util.Map;
//
///**
// * @author zhangrui
// * @date 2020-04-23 20:02
// * @since 1.0
// */
//public class MyHashMap {
//
//    //默认的初始容量为16，必须为2的n次幂
//    static final int DEFAULT_INITIAL_CAPACITY = 16;
//    //最大容量，如果使用有参构造指定具体的容量大于最大容量，则使用最大容量。且必须是2的n次幂。
//    static final int MAXIMUM_CAPACITY = 1 << 30;
//    //默认的负载因子
//    static final float DEFAULT_LOAD_FACTOR = 0.75f;
//    //键值对表，可根据需要调整大小，但长度必须为2的n次幂
//    transient Entry<K,V>[] table;
//    //此映射中包含的键值映射的数量
//    transient int size;
//    //容量阈值（一旦超过阈值就要扩容），等于 容量*负载因子
//    int threshold;
//    //记录修改次数
//    transient int modCount
//    //表示在对字符串键（即key为String类型）的HashMap应用替代哈希函数时HashMap的条目数量的默认阈值。替代哈希函数的使用可以减少由于对字符串键进行弱哈希码计算时的碰撞概率
//    static final int ALTERNATIVE_HASHING_THRESHOLD_DEFAULT = Integer.MAX_VALUE;
//    //如果useAltHashing为true，执行String类型键的替代哈希以减少由于哈希码计算较弱导致的冲突发生率。
//    transient boolean useAltHashing;
//    //与实例关联的随机值，用于哈希代码中，以减少冲突
//    transient final int hashSeed = 0;
//
//    public MyHashMap(int initialCapacity, float loadFactor) {
//        if (initialCapacity < 0)
//            throw new IllegalArgumentException("Illegal initial capacity: " +
//                    initialCapacity);
//        if (initialCapacity > MAXIMUM_CAPACITY)
//            initialCapacity = MAXIMUM_CAPACITY;
//        if (loadFactor <= 0 || Float.isNaN(loadFactor))
//            throw new IllegalArgumentException("Illegal load factor: " +
//                    loadFactor);
//        // Find a power of 2 >= initialCapacity
//        int capacity = 1;
//        while (capacity < initialCapacity) {
//            capacity <<= 1;
//        }
//        this.loadFactor = loadFactor;
//        threshold = (int)Math.min(capacity * loadFactor, MAXIMUM_CAPACITY + 1);
//        table = new Entry[capacity];
//        useAltHashing = sun.misc.VM.isBooted() &&
//                (capacity >= Holder.ALTERNATIVE_HASHING_THRESHOLD);
//        init();
//    }
//
//    public MyHashMap(int initialCapacity) {
//        this(initialCapacity, DEFAULT_LOAD_FACTOR);
//    }
//
//    public MyHashMap() {
//        this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
//    }
//
//    static class Entry<K,V> implements Map.Entry<K,V> {
//        final K key;
//        V value;
//        Entry<K, V> next;
//        int hash;
//    }
//
//    private static class Holder{
//// Unsafe mechanics
//        /**
//         * Unsafe utilities
//         */
//        static final sun.misc.Unsafe UNSAFE;
//        /**
//         * Offset of "final" hashSeed field we must set in readObject() method.
//         */
//        static final long HASHSEED_OFFSET;
//        /**
//         * Table capacity above which to switch to use alternative hashing.
//         */
//        static final int ALTERNATIVE_HASHING_THRESHOLD;
//        static {
//            String altThreshold = java.security.AccessController.doPrivileged(
//                    new sun.security.action.GetPropertyAction(
//                            "jdk.map.althashing.threshold"));
//            int threshold;
//            try {
//                threshold = (null != altThreshold)
//                        ? Integer.parseInt(altThreshold)
//                        : ALTERNATIVE_HASHING_THRESHOLD_DEFAULT;
//                // disable alternative hashing if -1
//                if (threshold == -1) {
//                    threshold = Integer.MAX_VALUE;
//                }
//                if (threshold < 0) {
//                    throw new IllegalArgumentException("value must be positive integer.");
//                }
//            } catch(IllegalArgumentException failed) {
//                throw new Error("Illegal value for 'jdk.map.althashing.threshold'", failed);
//            }
//            ALTERNATIVE_HASHING_THRESHOLD = threshold;
//            try {
//                UNSAFE = sun.misc.Unsafe.getUnsafe();
//                HASHSEED_OFFSET = UNSAFE.objectFieldOffset(
//                        HashMap.class.getDeclaredField("hashSeed"));
//            } catch (NoSuchFieldException | SecurityException e) {
//                throw new Error("Failed to record hashSeed offset", e);
//            }
//        }
//    }
//
//    final int hash(Object k) {
//        int h = 0;
//        if (useAltHashing) {
//            if (k instanceof String) {
//                //return sun.misc.Hashing.stringHash32((String) k);
//                return k.hashCode();
//            }
//            h = hashSeed;
//        }
//
//        h ^= k.hashCode();
//        // This function ensures that hashCodes that differ only by
//        // constant multiples at each bit position have a bounded
//        // number of collisions (approximately 8 at default load factor).
//        h ^= (h >>> 20) ^ (h >>> 12);
//        return h ^ (h >>> 7) ^ (h >>> 4);
//    }
//
//
//}
